package com.wangkang.eduservice.controller;
import com.wangkang.commonutils.R;
import com.wangkang.commonutils.result.ResultCode;
import com.wangkang.eduservice.client.VodClient;
import com.wangkang.eduservice.entity.EduVideo;
import com.wangkang.eduservice.entity.form.EduVideoForm;
import com.wangkang.eduservice.enums.EduResultCode;
import com.wangkang.eduservice.service.EduVideoService;
import com.wangkang.servicebase.exceptionhandler.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author wangkang
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
@Api(tags = "课程小节管理")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    //注入VodClient接口
    @Autowired
    private VodClient vodClient;


    public EduVideoController(EduVideoService videoService) {
        this.videoService = videoService;
        this.vodClient = vodClient;
    }

    @ApiOperation("添加小节")
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideoForm videoForm){
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoForm,eduVideo);

        boolean saveResult = videoService.save(eduVideo);
        if (!saveResult){
            throw GuliException.from(EduResultCode.SAVE_ERROR);
        }
        return  R.ok();
    }

    @ApiOperation("更新小节")
    @PutMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideoForm videoForm){
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoForm,video);

        boolean updateResult = videoService.updateById(video);
        if (!updateResult){
            throw GuliException.from(EduResultCode.UPDATE_ERROR);
        }
        return  R.ok();
    }

    @ApiOperation("根据id查询小节")
    @GetMapping("/{id}")
    public R getVideoById(@PathVariable String id){
        EduVideo video = videoService.getById(id);
        return R.ok().data("video",video);
    }

    @ApiOperation("根据id删除视频")
    @DeleteMapping("/{id}")
    /**
     * 删除小节，同时删除对应的阿里云视频(远程调用removeAliyunVideo方法)
     * 要先删除视频，再删除小节
     * 若先删除小节，则就查不到小节内视频id了，就不能再删除视频了。
     */
    public R deleteVideo(@PathVariable  String id){

        //1.根据小节id获取视频id，调用方法实现删除
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();

        //如果小节里面存在视频，再执行删除小节内视频
        if(StringUtils.isNotBlank(videoSourceId)){
            //根据视频id  远程调用实现视频删除
            //VodClient中removeAliyunVideo方法的返回值是R，故赋值给R对象
            R resultVo = vodClient.removeAliyunVideo(videoSourceId);
            if(resultVo.getCode().equals(ResultCode.ERROR.getCode())){
                throw GuliException.from(ResultCode.ERROR);
            }
        }

        //2.删除小节
        videoService.removeById(id);
        return R.ok();
    }

}